package org.fronczak.anita.berlinclock.model;


public class BerlinClockTime {
    public static final int NUMBER_OF_LAMPS_IN_SECOND_HOURS_ROW = 4;
    public static final int NUMBER_OF_LAMPS_IN_FIRST_HOURS_ROW = 24 / (NUMBER_OF_LAMPS_IN_SECOND_HOURS_ROW + 1); // 4
    public static final int NUMBER_OF_LAMPS_IN_SECOND_MINUTES_ROW = 4;
    public static final int NUMBER_OF_LAMPS_IN_FIRST_MINUTES_ROW = 59 / (NUMBER_OF_LAMPS_IN_SECOND_MINUTES_ROW + 1); // 11

    private final int hours;
    private final int minutes;
    private final int seconds;

    public BerlinClockTime(int hours, int minutes, int seconds) {
        if (hours < 0 || hours > 24) {
            throw new IllegalArgumentException("Hours should be in range <0, 24> inclusive");
        }
        if (hours == 24 && (minutes != 0 || seconds != 0)) {
            throw new IllegalArgumentException("You can only represent hours until 24:00:00");
        }
        if (minutes < 0 || minutes > 59) {
            throw new IllegalArgumentException("Minutes should be in range <0, 59> inclusive");
        }
        if (seconds < 0 || seconds > 59) {
            throw new IllegalArgumentException("Seconds should be in range <0, 59> inclusive");
        }

        this.hours = hours;
        this.minutes = minutes;
        this.seconds = seconds;
    }

    /**
     * Seconds lamp lits when number of seconds is even
     */
    public boolean isSecondsLampLit() {
        return seconds % 2 == 0;
    }

    /**
     * Every lit first row hour lamp represents 5 hours
     */
    public int getNumberOfFirstRowHourLampsLit() {
        return hours / (NUMBER_OF_LAMPS_IN_SECOND_HOURS_ROW + 1); // minutes / 5
    }

    /**
     * Every lit second row hour lamp represents 1 hour
     */
    public int getNumberOfSecondRowHourLampsLit() {
        return hours % (NUMBER_OF_LAMPS_IN_SECOND_HOURS_ROW + 1); // minutes % 5
    }

    /**
     * Every lit first row minute lamp represents 5 minutes
     */
    public int getNumberOfFirstRowMinuteLampsLit() {
        return minutes / (NUMBER_OF_LAMPS_IN_SECOND_MINUTES_ROW + 1); // minutes / 5
    }

    /**
     * Every lit second row hour lamp represents 1 minute
     */
    public int getNumberOfSecondRowMinuteLampsLit() {
        return minutes % (NUMBER_OF_LAMPS_IN_SECOND_MINUTES_ROW + 1); // minutes % 5
    }

    @Override
    public int hashCode() {
        return seconds + minutes * 60 + hours * 3600; // number of seconds
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        BerlinClockTime other = (BerlinClockTime) obj;
        return this.hashCode() == other.hashCode();
    }

}
